package com.dure.commonlib.util;

import android.app.Activity;
import android.app.NotificationManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ServiceInfo;
import android.net.Uri;
import android.os.Build;
import android.support.v4.content.FileProvider;
import android.util.Log;

import java.io.File;

/**
 * Created by leix on 10/1/16.
 */

public class ApplicationUtils {

    public static void installApk(String packageName, String urlPath, Context context,
                                  NotificationManager mNotificationManager, int NOTIFYCATIONID) {
        Intent apkIntent = new Intent();
        apkIntent.setAction(Intent.ACTION_VIEW);
        File apkFile = new File(urlPath);
        Log.i("jone", "apk length " + apkFile.length() + "");

        //判断是否是AndroidN以及更高的版本
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Uri contentUri = FileProvider.getUriForFile(context, packageName + ".fileProvider", apkFile);
            apkIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            apkIntent.setDataAndType(contentUri, "application/vnd.android.package-archive");
        } else {
            apkIntent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
            apkIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(apkIntent);
        mNotificationManager.cancel(NOTIFYCATIONID);
    }

    public static String getAppVersion(Context context) {
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            String version = info.versionCode +"";
            return version;
        } catch (Exception e) {
            e.printStackTrace();

        }

        return "";
    }

    public static String getAppVersionName(Context context) {
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            String version = info.versionName;
            return version;
        } catch (Exception e) {
            e.printStackTrace();

        }

        return "";
    }

    public static String getApplicationMetaData(Activity context, String name){
        ApplicationInfo appInfo = null;
        try {
            appInfo = context.getPackageManager()
                    .getApplicationInfo(context.getPackageName(),
                            PackageManager.GET_META_DATA);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        String msg="";
        if (appInfo != null) {
            msg = appInfo.metaData.getString(name);
        }

        return msg;
    }

    public static String getActivityMetaData(Activity context, String name){
        ActivityInfo info= null;
        try {
            info = context.getPackageManager()
                    .getActivityInfo(context.getComponentName(),
                            PackageManager.GET_META_DATA);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        String msg=info.metaData.getString(name);

        return msg;
    }

    public static String getServiceMetaData(Context context, String name, Class serviceClass){
        ComponentName cn=new ComponentName(context, serviceClass);
        ServiceInfo info= null;
        try {
            info = context.getPackageManager()
                    .getServiceInfo(cn, PackageManager.GET_META_DATA);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        String msg=info.metaData.getString(name);

        return msg;
    }

    public static String getReceiverMetaData(Context context, String name, Class receiverClass){
        ComponentName cn=new ComponentName(context, receiverClass);
        ActivityInfo info= null;
        try {
            info = context.getPackageManager()
                    .getReceiverInfo(cn, PackageManager.GET_META_DATA);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        String msg=info.metaData.getString(name);

        return msg;
    }
}
